package com.ihd.ihardware.view.tzc.health.view;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.common.adapter.GridDivider;
import com.ihd.ihardware.common.utils.CalUtils;
import com.ihd.ihardware.common.widget.ObservableScrollView;
import com.ihd.ihardware.common.widget.ShareDialog;
import com.ihd.ihardware.databinding.ActivityReportBinding;
import com.ihd.ihardware.pojo.NormRes;
import com.ihd.ihardware.pojo.ReportRes;
import com.ihd.ihardware.pojo.Test_rp;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.tzc.health.viewmodel.ReportViewModel;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;

import java.io.Serializable;

import cn.wowjoy.commonlibrary.adapter.CommonAdapter;
import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.DateUtils;
import cn.wowjoy.commonlibrary.utils.DensityUtil;
import cn.wowjoy.commonlibrary.utils.DialogUtils;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ShotImageUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import cn.wowjoy.commonlibrary.widget.titlebar.TitleBar;

import static com.ihd.ihardware.app.MyApplication.api;

public class ReportActivity extends BaseActivity<ActivityReportBinding, ReportViewModel> {

    private ReportRes.DataBean rd;
    private UserInfoRes.DataBean ud;
    private Test_rp index;
    private ShareDialog mBottomDG;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(false).init();
        binding.mtitlebar.setLeftBack(this);
        binding.mtitlebar.setLeftImageResource(R.drawable.ic_back_w);
        binding.mtitlebar.addAction(new TitleBar.Action() {
            @Override
            public String getText() {
                return null;
            }

            @Override
            public int getDrawable() {
                return R.drawable.navbtn_share_white;
            }

            @Override
            public void performAction(View view) {
                Bitmap bm = ShotImageUtils.shotScrollView(binding.shareSV);
                mBottomDG = new ShareDialog(ReportActivity.this, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mBottomDG.cancel();
                        ToastUtils.showShort(ReportActivity.this, "分享中...");

                        WXImageObject imgObj = new WXImageObject(bm);
                        WXMediaMessage msg = new WXMediaMessage();
                        msg.mediaObject = imgObj;
                        bm.recycle();
                        SendMessageToWX.Req req = new SendMessageToWX.Req();
                        req.transaction = buildTransaction("imgshareappdata");
                        req.message = msg;
                        req.scene = 0;
                        api.sendReq(req);
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mBottomDG.cancel();
                        ToastUtils.showShort(ReportActivity.this, "分享中...");
                        WXImageObject imgObj = new WXImageObject(bm);
                        WXMediaMessage msg = new WXMediaMessage();
                        msg.mediaObject = imgObj;
                        bm.recycle();
                        SendMessageToWX.Req req = new SendMessageToWX.Req();
                        req.transaction = buildTransaction("imgshareappdata");
                        req.message = msg;
                        req.scene = 1;
                        api.sendReq(req);
                    }
                });
                mBottomDG.show();
            }
        });

        rd = (ReportRes.DataBean) getIntent().getSerializableExtra("report");

        binding.dateTV.setText(rd.getCreateTime());
        binding.bpRV.setAdapter(viewModel.mRPAdapter);
        binding.bpRV.addItemDecoration(new GridDivider(this, 1, getResources().getColor(R.color.C_E8E9F0)));
        binding.bpRV.setHasFixedSize(true);
        binding.bpRV.setNestedScrollingEnabled(false);
        if (rd.getBmi() < 18.5) {
            binding.weightSB.setProgress((int) ((rd.getBmi() - 8.5) * 2));
        } else if (rd.getBmi() >= 18.5 && rd.getBmi() < 24) {
            binding.weightSB.setProgress((int) ((rd.getBmi() - 18.5) / 5.5 * 20) + 20);
        } else if (rd.getBmi() >= 24 && rd.getBmi() < 28) {
            binding.weightSB.setProgress((int) ((rd.getBmi() - 24) / 4 * 20) + 40);
        } else if (rd.getBmi() >= 28 && rd.getBmi() < 30) {
            binding.weightSB.setProgress((int) ((rd.getBmi() - 28) / 2 * 20) + 60);
        } else {
            binding.weightSB.setProgress((int) ((rd.getBmi() - 30) / 10 * 20) + 80);
        }
        binding.weightSB.setEnabled(false);

        ud = new Gson().fromJson(SPUtils.getString(AppConstans.USER_INFO, ""), UserInfoRes.DataBean.class);
        if ("1".equals(ud.getSex()))
            binding.sexImg.setImageResource(R.drawable.icon_boy);
        else
            binding.sexImg.setImageResource(R.drawable.icon_girl);
        binding.ts.setText(CalUtils.getTS(ud.getSex(), ud.getWeight(), ud.getHeight(), DateUtils.getAge(ud.getBirthDay())) + "g");
        binding.zf.setText(CalUtils.getZF(ud.getSex(), ud.getWeight(), ud.getHeight(), DateUtils.getAge(ud.getBirthDay())) + "g");
        binding.dbz.setText(CalUtils.getDBZ(ud.getSex(), ud.getWeight(), ud.getHeight(), DateUtils.getAge(ud.getBirthDay())) + "g");

        binding.hint0.setText(rd.getWeightChange() < 0 ? ("对比上次轻了" + -rd.getWeightChange() + "公斤") : ("对比上次重了" + rd.getWeightChange() + "公斤"));
        binding.weightTV.setText(String.valueOf(rd.getWeight()).split("\\.")[0]);
        binding.weightPointTV.setText("." + String.valueOf(rd.getWeight()).split("\\.")[1]);
        binding.hint.setText(rd.getSuggestion());

        viewModel.mRPAdapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                index = ((Test_rp) viewModel.mRPAdapter.getItem(position));
                viewModel.getNorm(rd.getId());
            }
        });

        setRx(AppConstans.NORM, new BaseConsumer<NormRes>() {
            @Override
            protected void onStart() {
                DialogUtils.waitingDialog(ReportActivity.this);
            }

            @Override
            protected void onSuccess(NormRes normRes) {
                DialogUtils.dismiss(ReportActivity.this);
                Intent i = new Intent(ReportActivity.this, DataDetailActivity.class);
                i.putExtra("raw", viewModel.mRPAdapter.getItems());
                i.putExtra("data", (Serializable) normRes.getData());
                i.putExtra("index", index);
                startActivity(i);
            }

            @Override
            protected void onError(Throwable error) {
                DialogUtils.dismiss(ReportActivity.this);
                ToastUtils.showShort(ReportActivity.this, error.getMessage());
            }
        });

        binding.titleFL.getBackground().setAlpha(0);
        binding.shareSV.setOnScollChangedListener(new ObservableScrollView.OnScollChangedListener() {
            @Override
            public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
                int alpha = y * 255 / DensityUtil.dip2px(166);
                binding.titleFL.getBackground().setAlpha(alpha > 255 ? 255 : alpha);
            }
        });

        viewModel.getRPList(rd);
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_report;
    }

    @Override
    protected Class getViewModelClass() {
        return ReportViewModel.class;
    }

}
